<?php
class Check extends Controller {
	private $tpl = 'order/check.html';

	public function html() {
		$tid = $this->post('tid', 'int');
		if(!$tid) header("Location: /category/");
		
		$mUser = new User_model();
		$currUser = $mUser->currUser();
		if(!$currUser) header('Localhost:/user/login/?backurl='.urlencode('/deal/buy/'.$tid.'.html'));
		
		//订单数据
		$num = $this->post('num', 'int');
		if(!$num) $this->display(array('status' => '-1', 'message' => '请输入数量'), $this->tpl);
		
		$ticket_type = $this->post('ticket_type', 'int');
		if($ticket_type) {
			$gp = $this->post('gp', 'int');
			$gt = $this->post('gt', 'int');
			$gc = $this->post('gc', 'int');
			if(!$gp || !$gt || !$gc) $this->display(array('status' => '-5', 'message' => '请输入名称'), $this->tpl);
			
			$location = $this->post('location', 'str');
			if(!$location) $this->display(array('status' => '-6', 'message' => '请输入地址'), $this->tpl);
			
			$receiver = $this->post('receiver', 'str');
			if(!$receiver) $this->display(array('status' => '-7', 'message' => '请输入收件人'), $this->tpl);
			
			$post = $this->post('post', 'str');
			if(!$post) $this->display(array('status' => '-8', 'message' => '请输入邮编'), $this->tpl);
			
			$receive_mode = $this->post('receive_mode', 'int');
			$receive_comment = $this->post('$receive_comment', 'str');
		} else {
			$name = $this->post('name', 'str');
			if(!$name) $this->display(array('status' => '-2', 'message' => '请输入名称'), $this->tpl);
		
			$tellphone = $this->post('tellphone', 'str');
			if(!Validate::isPhone($tellphone)) $this->display(array('status' => '-3', 'message' => '手机号输入错误'), $this->tpl);
			if(!$tellphone) $this->display(array('status' => '-4', 'message' => '请输入手机号'), $this->tpl);
		}
		
		//获取门票信息
		$mTicket = new Ticket_model();
		$params = array('tid' => $tid);
		$data['ticket'] = $mTicket->info($params);
		if($data['ticket']) {
			$mProduct = new Product_model();
			$data['product'] = $mProduct->productInfo(array('id' => $data['ticket']['product_id']));
			
			if($data['product']) {
				//生成订单
				$mOrder = new Order_model();
				$oid = $mOrder->oid($currUser['uid']);
				if($ticket_type) {
					$params = array(
						'oid' => $oid,
						'uid' => $currUser['uid'],
						'ticket_id' => $tid,
						'price' => $data['ticket']['market_price'],
						'num' => $num,
						'product_id' => $data['product']['id'],
						'receiver' => $receiver,
						'gp' => $gp,
						'gt' => $gt,
						'gc' => $gc,
						'location' => $location,
						'receiver' => $receiver,
						'post' => $post,
						'receive_mode' => $receive_mode,
						'receive_comment' => $receive_comment,
						'created_time' => Func::time()
					);
				} else {
					$params = array(
						'oid' => $oid,
						'uid' => $currUser['uid'],
						'ticket_id' => $tid,
						'price' => $data['ticket']['market_price'],
						'num' => $num,
						'product_id' => $data['product']['id'],
						'name' => $name,
						'tellphone' => $tellphone,
						'created_time' => Func::time()
					);
				}
				$id = $mOrder->add($params);
				if($id) $this->display(array('status' => '0', 'message' => '下单成功'), $this->tpl);
				else $this->display(array('status' => '-9', 'message' => '下单失败'), $this->tpl);
			}
		}
		$this->display($data, $tpl);
	}
}